package com.example.reagent_management.room.dao;

import com.example.reagent_management.room.entity.GetAllCounts;
import com.example.reagent_management.room.entity.InStoreChoose;
import com.example.reagent_management.room.entity.OperateInfoQuery;
import com.example.reagent_management.room.entity.ReagentOperate;
import com.example.reagent_management.ui.activity.pojo.RemindDateAndCount;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import androidx.lifecycle.LiveData;
import androidx.paging.PagingSource;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;
import kotlinx.coroutines.flow.Flow;

@Dao
public interface StoreDao {

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    Long insertReagent(ReagentOperate reagentOperate);

    /**
     * @param batch 批号
     * @param reagent_id 试剂信息id
     * @param operate_id 操作类型
     * @return
     */
    @Query("select counts,id from operate " +
            "where batch_number = :batch and reagent_info =:reagent_id and operate = :operate_id " +
            "order by counts desc limit 1 ")
    GetAllCounts getInStoreCounts(String batch, Long reagent_id, Integer operate_id);

    /*
    * 获取入库的试剂批号 b去重distinct
    * */
    @Query("select id,batch_number,days,num,supplier from operate" +
            " where operate = 1 and reagent_info = :reagent_id")
    PagingSource<Integer, InStoreChoose> getBatchList(Long reagent_id);

//    @Update() 更新整行 2.2之后也可更新部分参数，暂时没测试

    /**
     * @param counts 数量
     * @param id 入库ID
     * @return
     */
    @Query("update operate set num =:counts where id = :id")
    Integer updateCounts(Long counts,Long id);

    @Query("select num,supplier from operate where id = :id")
    OperateInfoQuery getSupplierAndNum(Long id);

    @Query("delete from operate where id = :id")
    Integer deleteOperateById(Long id);

    @Query("select * from operate where id = :id")
    ReagentOperate getOperate(long id);

    @Query("update operate set counts = :counts where id = :id")
    Integer updateLastCounts(long id,long counts);

    @Query("select num from operate where id = :id")
    Long getNumById(long id);

    @Query("select counts from operate where id = :id")
    Long getCounts(long id);


    /**
     * 查询用于提醒的数据
     */
    @Query("select id, project_name,days,reagent_name,num from operate" +
            " left join reagent_information as reagent on operate.reagent_info = reagent.reagent_id" +
            " where operate = 1")
    LiveData<List<RemindDateAndCount>> getRemindInfo();

    @Query("delete from operate where days < datetime('now', 'localtime', '-31 days') and operate = 1 and num = 0")
    void cleanInStore();

    @Query("delete from operate where days < datetime('now', 'localtime', '-31 days') and operate = 0")
    void cleanOutStore();
}
